package com.itheima.sh.controller;

import com.itheima.sh.pojo.User;
import com.itheima.sh.response.Result;
import com.itheima.sh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Restful 风格
 * @Description:
 * @Version: V1.0
 */
//@Controller  // 必须是Controller
//@ResponseBody   // response从写 write
@RestController
// （对象类型 --> JSON，并且设置contentType:application/josn）
// String：直接通过write方法响应给浏览器，并且设置contentType:text/html
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping
    public Result findAll(){

//        try {
//            return Result.okResult(userService.findAll());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.errorResult(StatusCode.ERROR);
//        }
        return Result.okResult(userService.findAll());
    }

    // @RequestParam : key=value&key=value
    // @RequestBody : {}  []
    @PostMapping  // POST 请求
    public Result add( @RequestBody User user) {



        boolean b = userService.add(user);
        return Result.okResult(user);
    }

}
